<?php
/*
 * @Author: zwx
 * @Date: 2024-05-08 10:39:41
 * @LastEditors: zwx
 * @LastEditTime: 2024-06-26 17:49:26
 * @Description: 私域直播严禁词
 */

namespace app\apiadmin\controller\live;

use app\apiadmin\command\getToken;
use think\facade\Db;
use app\common\utils\Res;
use app\common\controller\BaseController;
use app\apiadmin\service\redis\MsgService;
use app\Request;
use Exception;

class Yanjincigroup extends BaseController
{



    /**
     * @author: zwx
     * @Description: 获取严禁词组列表
     * @Date: 2024-05-08 15:41:00
     */
    public function getdatagroup()
    {
        $param = $this->request->param();

        $size = $param['size'] ? $param['size'] : 10;  //每页显示条数
        $page = $param['page'] ? $param['page'] : 1;  //当前页数

        $input = $param['input'] ? $param['input'] : false;  //输入的内容




        $where = array();
        $where[] = ['del', '=', 0];
        if ($input) {
            $where[] = array('name', 'like', '%' . $input . '%');
        }


        $data = array();
        $result = DB::table('xh_siyu_yanjinci_group')->where($where)->order('id desc')->page($page, $size)->select();
        $count = DB::table('xh_siyu_yanjinci_group')->where($where)->count();
        $data = [
            'list' => $result,
            'total' => $count
        ];

        return Res::ok($data);
    }

    /**
     * @author: zwx
     * @Description: 创建严禁词组
     * @Date: 2024-05-09 11:21:47
     */
    public function createYanjincigroup()
    {

        $param = $this->request->param();
        $name = $param['name'];  //名称


        if (!$name || $name == '') {
            return Res::error('名称不能为空');
        }

        $data = array();
        $data['name'] = $name;
        $data['createtime'] = date('Y-m-d H:i:s');
        $id = DB::table('xh_siyu_yanjinci_group')->insert($data);

        return Res::ok($id);

    }


    /**
     * @author: zwx
     * @Description: 删除严禁词组
     * @Date: 2024-05-09 14:16:52
     */
    public function delYanjincigroup()
    {

        $param = $this->request->param();
        $id = $param['id'];

        $data = DB::table('xh_siyu_yanjinci_group')->where(array('id' => $id))->find();
        if ($data) {
            $a = DB::table('xh_siyu_yanjinci_group')->where(array('id' => $id))->update(array('del' => 1));
            return Res::ok($a);
        } else {
            return Res::error('未查询到数据，请刷新重试');
        }

    }

    /**
     * @author: zwx
     * @Description: 修改严禁词组
     * @Date: 2024-05-09 11:21:47
     */
    public function editYanjincigroup()
    {

        $param = $this->request->param();
        $id = $param['id'];
        $name = $param['name'];  //名称

        $data = array();
        $data['name'] = $name;

        $dd = DB::table('xh_siyu_yanjinci_group')->where(array('id' => $id))->find();

        if ($dd) {
            $a = DB::table('xh_siyu_yanjinci_group')->where(array('id' => $id))->save($data);
            return Res::ok($a);
        } else {
            return Res::error('未查询到数据，请刷新重试');
        }

        return Res::ok($id);

    }


    /**
     * @author: zwx
     * @Description: 批量删除严禁词组
     * @Date: 2024-05-09 14:16:52
     */
    public function pldelYanjincigroup()
    {

        $param = $this->request->param();
        $ids = $param['ids'];

        $where = array();
        $where[] = ['id', 'in', $ids];

        $data = DB::table('xh_siyu_yanjinci_group')->where($where)->select();
        if ($data) {
            $a = DB::table('xh_siyu_yanjinci_group')->where($where)->update(array('del' => 1));
            return Res::ok($a);
        } else {
            return Res::error('未查询到数据，请刷新重试');
        }

    }


    /************************     严禁词                *****************************888 */

    /**
     * @author: zwx
     * @Description: 获取严禁词列表
     * @Date: 2024-05-08 15:41:00
     */
    public function getdata()
    {
        $param = $this->request->post();

        $size = $param['size'] ? $param['size'] : 10;  //每页显示条数
        $page = $param['page'] ? $param['page'] : 1;  //当前页数

        $input = $param['input'] ? $param['input'] : false;  //输入的内容

        $groupid = $param['groupid'] ? $param['groupid'] : 0;  //严禁词组ID




        $where = array();
        $where[] = ['del', '=', 0];
        if ($input) {
            $where[] = array('name', 'like', '%' . $input . '%');
        }

        if ($groupid > 0) {
            $where[] = ['groupid', '=', $groupid];
        }


        $data = array();
        $result = DB::table('xh_siyu_yanjinci_list')->where($where)->order('id desc')->page($page, $size)->select();
        $count = DB::table('xh_siyu_yanjinci_list')->where($where)->count();

        $data = [
            'list' => $result,
            'total' => $count,
        ];



        return Res::ok($data);
    }

    /**
     * @author: zwx
     * @Description: 创建严禁词
     * @Date: 2024-05-09 11:21:47
     */
    public function createYanjinci()
    {

        $param = $this->request->param();
        $name = $param['name'];  //名称
        $groupid = $param['groupid'];  //名称
        $liveId = $param['liveId'] ?? 0;  //直播ID

        if (!$name || $name == '') {
            return Res::error('名称不能为空');
        }

        $data = array();
        $data['name'] = $name;
        $data['groupid'] = $groupid;
        $data['createtime'] = date('Y-m-d H:i:s');
        $id = DB::table('xh_siyu_yanjinci_list')->insert($data);

        if (!empty($id) && $liveId > 0) {
            //$sendRedis = MsgService::sendRedisReloadLiveMessage($liveId);
            $sendRedis = MsgService::sendAdd($liveId, $name);
            $id = ['id' => $id, 'redis' => $sendRedis];
        }

        return Res::ok($id);

    }



    /**
     * @author: zwx
     * @Description: 删除严禁词
     * @Date: 2024-05-09 14:16:52
     */
    public function delYanjinci()
    {

        $param = $this->request->param();
        $id = $param['id'];
        $liveId = $param['liveId'] ?? 0;  //直播ID

        $data = DB::table('xh_siyu_yanjinci_list')->where(array('id' => $id))->find();
        if ($data) {
            $a = DB::table('xh_siyu_yanjinci_list')->where(array('id' => $id))->update(array('del' => 1));
            if (!empty($a) && $liveId > 0) {
                //$sendRedis = MsgService::sendRedisReloadLiveMessage($liveId);
                $sendRedis = MsgService::sendDel($liveId, $data['name']);
                $a = ['a' => $a, 'redis' => $sendRedis];
            }
            return Res::ok($a);
        } else {
            return Res::error('未查询到数据，请刷新重试');
        }

    }

    /**
     * @author: zwx
     * @Description: 修改严禁词
     * @Date: 2024-05-09 11:21:47
     */
    public function editYanjinci()
    {

        $param = $this->request->param();
        $id = $param['id'];
        $name = $param['name'];  //名称
        $liveId = $param['live_id'] ?? 0;  //直播ID

        $data = array();
        $data['name'] = $name;

        $dd = DB::table('xh_siyu_yanjinci_list')->where(array('id' => $id))->find();

        if ($dd) {
            $a = DB::table('xh_siyu_yanjinci_list')->where(array('id' => $id))->save($data);
            if (!empty($a) && $liveId > 0) {
                MsgService::sendRedisReloadLiveMessage($liveId);
            }
            return Res::ok($a);
        } else {
            return Res::error('未查询到数据，请刷新重试');
        }

        return Res::ok($id);

    }


    /**
     * @author: zwx
     * @Description: 批量删除严禁词
     * @Date: 2024-05-09 14:16:52
     */
    public function pldelYanjinci()
    {

        $param = $this->request->param();
        $ids = $param['ids'];
        $liveId = $param['live_id'] ?? 0;  //直播ID

        $where = array();
        $where[] = ['id', 'in', $ids];

        $data = DB::table('xh_siyu_yanjinci_list')->where($where)->select();
        if ($data) {
            $a = DB::table('xh_siyu_yanjinci_list')->where($where)->update(array('del' => 1));
            if (!empty($a) && $liveId > 0) {
                MsgService::sendRedisReloadLiveMessage($liveId);
            }
            return Res::ok($a);
        } else {
            return Res::error('未查询到数据，请刷新重试');
        }

    }

}


